'use strict';

const Controller = require('egg').Controller;
const md5 = require('md5')
const jwt = require('jsonwebtoken')

class UserController extends Controller {
    async register() { //注册接口
        console.time('q1')
        const registerRule = {
            adminName: {
                type: 'string'
            },
            adminPsd: {
                type: 'password'
            },
            adminPsd2: {
                type: 'password'
            }
        }
        let body = this.ctx.request.body;
        let checkName = await this.service.mysql.find('adminuser', {
            adminName: body.adminName
        })
        if (checkName) {
            this.ctx.helper.resultData(-1, null, '用户名重复')
        } else {
            this.ctx.validate(registerRule, body)
            if (body.adminPsd !== body.adminPsd2) {
                this.ctx.helper.resultData(-1, null, '两次密码输入不一致')
            } else {
                let insertStatus = await this.service.mysql.insert('adminuser', {
                    adminName: body.adminName,
                    adminPsd: md5(body.adminPsd),
                    loginTime: new Date().getTime(),
                    addTime: new Date().getTime(),
                    status: 0
                })
                if (insertStatus) {
                    let result = await this.service.mysql.find('adminuser', {
                        adminName: body.adminName,
                        adminPsd: md5(body.adminPsd)
                    })
                    const token = jwt.sign({
                        id: result.id,
                        adminName: result.adminName
                    }, this.config.jwt.jwtKey, {
                        expiresIn: '60min'
                    })
                    this.ctx.helper.resultData(0, {
                        token: token
                    }, '注册成功')
                    this.ctx.set('token',token)

                } else {
                    this.ctx.helper.resultData(-1, null, '注册失败')
                }
            }
        }
        console.timeEnd('q1')
    }

    async login() { //登录接口
        const loginRule = {
            adminName: {
                type: 'string'
            },
            adminPsd: {
                type: 'password'
            }
        }
        let body = this.ctx.request.body;
        this.ctx.validate(loginRule, body)
        let result = await this.service.mysql.find('adminuser', {
            adminName: body.adminName,
            adminPsd: md5(body.adminPsd)
        })
        if (result) {
            const token = jwt.sign({
                id: result.id,
                adminName: result.adminName
            }, this.config.jwt.jwtKey, {
                expiresIn: '60min'
            })
            this.ctx.helper.resultData(0, {
                token: token
            }, 'ok')
            result.loginTime = new Date().getTime();
            this.service.mysql.update('adminuser', result)
        } else {
            this.ctx.helper.resultData(-1, null, '用户名或密码错误')
        }
    }

    async userInfo() { //查询用户信息
        let tokenContent = await jwt.verify(this.ctx.header.authorization, this.config.jwt.jwtKey); //如果 token 过期或验证失败，将返回401
        let userInfo = await this.service.mysql.find('adminuser', {
            id: tokenContent.id
        })
        this.ctx.helper.resultData(0, {
            userInfo: {
                id: userInfo.id,
                adminName: userInfo.adminName,
                status: userInfo.status
            },
            roles: 'admin'
        }, 'successs')
    }
}

module.exports = UserController;